package com.easemob.chat;

import com.easemob.chat.core.p;
import com.easemob.util.EMLog;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Message.Body;
import org.jivesoftware.smackx.packet.DelayInfo;
import org.json.JSONArray;
import org.json.JSONObject;

public class MessageEncoder
{
  private static final String TAG = "encoder";
  public static final String ATTR_TYPE = "type";
  public static final String ATTR_TO = "to";
  public static final String ATTR_FROM = "from";
  public static final String ATTR_MSG = "msg";
  public static final String ATTR_URL = "url";
  public static final String ATTR_LOCALURL = "localurl";
  public static final String ATTR_THUMB_LOCALURL = "thumblocalurl";
  public static final String ATTR_FILENAME = "filename";
  public static final String ATTR_THUMBNAIL = "thumb";
  public static final String ATTR_SECRET = "secret";
  public static final String ATTR_SIZE = "size";
  public static final String ATTR_IMG_WIDTH = "width";
  public static final String ATTR_IMG_HEIGHT = "height";
  public static final String ATTR_THUMBNAIL_SECRET = "thumb_secret";
  public static final String ATTR_LENGTH = "length";
  public static final String ATTR_ADDRESS = "addr";
  public static final String ATTR_LATITUDE = "lat";
  public static final String ATTR_LONGITUDE = "lng";
  public static final String ATTR_ACTION = "action";
  public static final String ATTR_PARAM = "param";
  public static final String ATTR_FILE_LENGTH = "file_length";
  public static final String ATTR_EXT = "ext";
  private static final String ATTR_TYPE_TXT = "txt";
  private static final String ATTR_TYPE_IMG = "img";
  private static final String ATTR_TYPE_VOICE = "audio";
  private static final String ATTR_TYPE_VIDEO = "video";
  private static final String ATTR_TYPE_LOCATION = "loc";
  private static final String ATTR_TYPE_CMD = "cmd";
  private static final String ATTR_TYPE_file = "file";

  public static String getJSONMsg(EMMessage paramEMMessage, boolean paramBoolean)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("{");
    localStringBuffer.append("\"from\":\"" + paramEMMessage.from.username + "\",");
    localStringBuffer.append("\"to\":\"" + paramEMMessage.to.username + "\",");
    localStringBuffer.append("\"bodies\":[{");
    if (paramEMMessage.type == EMMessage.Type.TXT)
      addTxtBody(localStringBuffer, paramEMMessage);
    else if (paramEMMessage.type == EMMessage.Type.IMAGE)
      addImageBody(localStringBuffer, paramEMMessage, paramBoolean);
    else if (paramEMMessage.type == EMMessage.Type.VOICE)
      addVoiceBody(localStringBuffer, paramEMMessage, paramBoolean);
    else if (paramEMMessage.type == EMMessage.Type.LOCATION)
      addLocationBody(localStringBuffer, paramEMMessage);
    else if (paramEMMessage.type == EMMessage.Type.CMD)
      addCmdBody(localStringBuffer, paramEMMessage);
    else if (paramEMMessage.type == EMMessage.Type.VIDEO)
      addVideoBody(localStringBuffer, paramEMMessage, paramBoolean);
    else if (paramEMMessage.type == EMMessage.Type.FILE)
      addFileBody(localStringBuffer, paramEMMessage, paramBoolean);
    localStringBuffer.append("}]");
    if (paramEMMessage.attributes != null)
      addExtAttr(localStringBuffer, paramEMMessage);
    localStringBuffer.append("}");
    localStringBuffer.toString();
    return localStringBuffer.toString();
  }

  private static void addExtAttr(StringBuffer paramStringBuffer, EMMessage paramEMMessage)
  {
    paramStringBuffer.append(",");
    paramStringBuffer.append("\"ext\":{");
    int i = 1;
    synchronized (paramEMMessage.attributes)
    {
      Iterator localIterator = paramEMMessage.attributes.keySet().iterator();
      while (localIterator.hasNext())
      {
        String str = (String)localIterator.next();
        paramStringBuffer.append("\"" + str + "\":");
        Object localObject1 = paramEMMessage.attributes.get(str);
        if ((localObject1 instanceof JSONObject))
        {
          paramStringBuffer.append(localObject1.toString());
        }
        else if ((localObject1 instanceof JSONArray))
        {
          paramStringBuffer.append(localObject1.toString());
        }
        else
        {
          Object localObject2;
          if ((localObject1 instanceof String))
          {
            localObject2 = (String)localObject1;
            if (((((String)localObject2).startsWith("{")) && (((String)localObject2).endsWith("}")) && (((String)localObject2).contains(":"))) || ((((String)localObject2).startsWith("[{")) && (((String)localObject2).endsWith("}]")) && (((String)localObject2).contains(":"))))
              paramStringBuffer.append(localObject1);
            else
              paramStringBuffer.append("\"" + localObject1 + "\"");
          }
          else if ((localObject1 instanceof Boolean))
          {
            localObject2 = (Boolean)localObject1;
            if (((Boolean)localObject2).booleanValue())
              paramStringBuffer.append("true");
            else
              paramStringBuffer.append("false");
          }
          else
          {
            localObject2 = (Integer)localObject1;
            paramStringBuffer.append(((Integer)localObject2).toString());
          }
        }
        if (i < paramEMMessage.attributes.size())
          paramStringBuffer.append(",");
        i++;
      }
    }
    paramStringBuffer.append("}");
  }

  private static void addImageBody(StringBuffer paramStringBuffer, EMMessage paramEMMessage, boolean paramBoolean)
  {
    paramStringBuffer.append("\"type\":\"img\",");
    ImageMessageBody localImageMessageBody = (ImageMessageBody)paramEMMessage.body;
    paramStringBuffer.append("\"url\":\"" + localImageMessageBody.remoteUrl + "\",");
    if (paramBoolean)
      paramStringBuffer.append("\"localurl\":\"" + localImageMessageBody.localUrl + "\",");
    paramStringBuffer.append("\"filename\":\"" + localImageMessageBody.fileName + "\",");
    if (localImageMessageBody.thumbnailUrl != null)
      paramStringBuffer.append("\"thumb\":\"" + localImageMessageBody.thumbnailUrl + "\",");
    paramStringBuffer.append("\"secret\":\"" + localImageMessageBody.secret + "\",");
    paramStringBuffer.append("\"size\":{\"width\":" + localImageMessageBody.width + ",\"" + "height" + "\":" + localImageMessageBody.height + "}");
    if (localImageMessageBody.thumbnailSecret != null)
      paramStringBuffer.append(",\"thumb_secret\":\"" + localImageMessageBody.thumbnailSecret + "\"");
  }

  private static void addVideoBody(StringBuffer paramStringBuffer, EMMessage paramEMMessage, boolean paramBoolean)
  {
    paramStringBuffer.append("\"type\":\"video\",");
    VideoMessageBody localVideoMessageBody = (VideoMessageBody)paramEMMessage.body;
    paramStringBuffer.append("\"url\":\"" + localVideoMessageBody.remoteUrl + "\",");
    if (paramBoolean)
    {
      paramStringBuffer.append("\"localurl\":\"" + localVideoMessageBody.localUrl + "\",");
      paramStringBuffer.append("\"thumblocalurl\":\"" + localVideoMessageBody.localThumb + "\",");
    }
    paramStringBuffer.append("\"filename\":\"" + localVideoMessageBody.fileName + "\",");
    paramStringBuffer.append("\"thumb\":\"" + localVideoMessageBody.thumbnailUrl + "\",");
    paramStringBuffer.append("\"length\":" + localVideoMessageBody.length + ",");
    paramStringBuffer.append("\"file_length\":" + localVideoMessageBody.file_length + ",");
    paramStringBuffer.append("\"secret\":\"" + localVideoMessageBody.secret + "\"");
    if (localVideoMessageBody.thumbnailSecret != null)
      paramStringBuffer.append(",\"thumb_secret\":\"" + localVideoMessageBody.thumbnailSecret + "\"");
  }

  private static void addTxtBody(StringBuffer paramStringBuffer, EMMessage paramEMMessage)
  {
    paramStringBuffer.append("\"type\":\"txt\",");
    TextMessageBody localTextMessageBody = (TextMessageBody)paramEMMessage.body;
    String str = localTextMessageBody.message;
    str = JSONObject.quote(str);
    if (((str.startsWith("{")) && (str.endsWith("}"))) || ((str.startsWith("[")) && (str.endsWith("]"))))
    {
      str = str.replaceAll("\"", "%22");
      paramStringBuffer.append("\"msg\":\"" + str + "\"");
    }
    paramStringBuffer.append("\"msg\":" + str);
  }

  private static void addCmdBody(StringBuffer paramStringBuffer, EMMessage paramEMMessage)
  {
    paramStringBuffer.append("\"type\":\"cmd\",");
    CmdMessageBody localCmdMessageBody = (CmdMessageBody)paramEMMessage.body;
    paramStringBuffer.append("\"action\":\"" + localCmdMessageBody.action + "\",");
    paramStringBuffer.append("\"param\":[");
    if ((localCmdMessageBody.params != null) && (localCmdMessageBody.params.size() != 0))
    {
      Iterator localIterator = localCmdMessageBody.params.entrySet().iterator();
      while (localIterator.hasNext())
      {
        Map.Entry localEntry = (Map.Entry)localIterator.next();
        paramStringBuffer.append("{\"");
        paramStringBuffer.append(localEntry.getKey());
        paramStringBuffer.append("\":\"");
        paramStringBuffer.append(localEntry.getValue());
        paramStringBuffer.append("\"},");
      }
      paramStringBuffer.deleteCharAt(paramStringBuffer.lastIndexOf(","));
    }
    paramStringBuffer.append("]");
  }

  private static void addVoiceBody(StringBuffer paramStringBuffer, EMMessage paramEMMessage, boolean paramBoolean)
  {
    paramStringBuffer.append("\"type\":\"audio\",");
    VoiceMessageBody localVoiceMessageBody = (VoiceMessageBody)paramEMMessage.body;
    paramStringBuffer.append("\"url\":\"" + localVoiceMessageBody.remoteUrl + "\",");
    if (paramBoolean)
      paramStringBuffer.append("\"localurl\":\"" + localVoiceMessageBody.localUrl + "\",");
    paramStringBuffer.append("\"filename\":\"" + localVoiceMessageBody.fileName + "\",");
    paramStringBuffer.append("\"length\":" + localVoiceMessageBody.length + ",");
    paramStringBuffer.append("\"secret\":\"" + localVoiceMessageBody.secret + "\"");
  }

  private static void addFileBody(StringBuffer paramStringBuffer, EMMessage paramEMMessage, boolean paramBoolean)
  {
    paramStringBuffer.append("\"type\":\"file\",");
    NormalFileMessageBody localNormalFileMessageBody = (NormalFileMessageBody)paramEMMessage.body;
    paramStringBuffer.append("\"url\":\"" + localNormalFileMessageBody.remoteUrl + "\",");
    if (paramBoolean)
      paramStringBuffer.append("\"localurl\":\"" + localNormalFileMessageBody.localUrl + "\",");
    paramStringBuffer.append("\"filename\":\"" + localNormalFileMessageBody.fileName + "\",");
    paramStringBuffer.append("\"file_length\":" + localNormalFileMessageBody.fileSize + ",");
    paramStringBuffer.append("\"secret\":\"" + localNormalFileMessageBody.secret + "\"");
  }

  private static void addLocationBody(StringBuffer paramStringBuffer, EMMessage paramEMMessage)
  {
    paramStringBuffer.append("\"type\":\"loc\",");
    LocationMessageBody localLocationMessageBody = (LocationMessageBody)paramEMMessage.body;
    paramStringBuffer.append("\"addr\":\"" + localLocationMessageBody.address + "\",");
    paramStringBuffer.append("\"lat\":" + localLocationMessageBody.latitude + ",");
    paramStringBuffer.append("\"lng\":" + localLocationMessageBody.longitude);
  }

  static EMMessage parseXmppMsg(Message paramMessage)
  {
    Object localObject2;
    Object localObject3;
    if (paramMessage.getExtension("encrypt", "jabber:client") != null)
    {
      EMLog.d("encoder", "it is encrypted message, decripting");
      try
      {
        String str1 = paramMessage.getBody();
        localObject1 = EMContactManager.getUserNameFromEid(paramMessage.getFrom());
        localObject2 = EMEncryptUtils.decryptMessage(str1, (String)localObject1);
        Iterator localIterator = paramMessage.getBodies().iterator();
        while (localIterator.hasNext())
        {
          localObject3 = (Message.Body)localIterator.next();
          paramMessage.removeBody((Message.Body)localObject3);
        }
        paramMessage.setBody((String)localObject2);
      }
      catch (Exception localException1)
      {
        localException1.printStackTrace();
      }
    }
    String str2 = paramMessage.getBody();
    if ((!str2.startsWith("{")) || (!str2.endsWith("}")))
    {
      EMLog.d("encoder", "msg not in json format, ignore");
      return null;
    }
    Object localObject1 = getMsgFromJson(str2);
    if (localObject1 == null)
    {
      EMLog.e("encoder", "wrong message format:" + paramMessage.toXML());
      return null;
    }
    try
    {
      localObject2 = (DelayInfo)paramMessage.getExtension("delay", "urn:xmpp:delay");
      if (localObject2 != null)
      {
        ((EMMessage)localObject1).msgTime = ((DelayInfo)localObject2).getStamp().getTime();
        ((EMMessage)localObject1).offline = true;
      }
      else
      {
        localObject3 = (p)paramMessage.getExtension("ts", "urn:xmpp:timestamp");
        if (localObject3 != null)
          ((EMMessage)localObject1).msgTime = ((p)localObject3).a().getTime();
      }
    }
    catch (Exception localException2)
    {
    }
    ((EMMessage)localObject1).direct = EMMessage.Direct.RECEIVE;
    ((EMMessage)localObject1).msgId = paramMessage.getPacketID();
    ((EMMessage)localObject1).status = EMMessage.Status.CREATE;
    ((EMMessage)localObject1).unread = true;
    return localObject1;
  }

  public static EMMessage getMsgFromJson(String paramString)
  {
    try
    {
      JSONObject localJSONObject1 = new JSONObject(paramString);
      EMContact localEMContact1 = new EMContact(localJSONObject1.getString("from"));
      EMContact localEMContact2 = new EMContact(localJSONObject1.getString("to"));
      JSONArray localJSONArray = localJSONObject1.getJSONArray("bodies");
      if (localJSONArray.length() < 1)
      {
        EMLog.d("encoder", "wrong msg without body");
        return null;
      }
      JSONObject localJSONObject2 = localJSONArray.getJSONObject(0);
      String str1 = localJSONObject2.getString("type");
      EMMessage localEMMessage = null;
      Object localObject1;
      String str2;
      Object localObject3;
      Object localObject2;
      Object localObject4;
      if (str1.equals("txt"))
      {
        localEMMessage = new EMMessage(EMMessage.Type.TXT);
        localObject1 = localJSONObject2.getString("msg");
        str2 = ((String)localObject1).replaceAll("%22", "\"");
        localObject3 = new TextMessageBody(str2);
        localEMMessage.addBody((MessageBody)localObject3);
      }
      else
      {
        Object localObject5;
        if (str1.equals("img"))
        {
          localEMMessage = new EMMessage(EMMessage.Type.IMAGE);
          localObject1 = localJSONObject2.getString("url");
          str2 = localJSONObject2.getString("filename");
          localObject3 = localObject1;
          if (localJSONObject2.has("thumb"))
            localObject3 = localJSONObject2.getString("thumb");
          ImageMessageBody localImageMessageBody = new ImageMessageBody(str2, (String)localObject1, (String)localObject3);
          if (localJSONObject2.has("localurl"))
            localImageMessageBody.localUrl = localJSONObject2.getString("localurl");
          if (localJSONObject2.has("secret"))
            localImageMessageBody.setSecret(localJSONObject2.getString("secret"));
          if (localJSONObject2.has("thumb_secret"))
            localImageMessageBody.setThumbnailSecret(localJSONObject2.getString("thumb_secret"));
          if (localJSONObject2.has("size"))
          {
            localObject5 = localJSONObject2.getJSONObject("size");
            localImageMessageBody.width = ((JSONObject)localObject5).getInt("width");
            localImageMessageBody.height = ((JSONObject)localObject5).getInt("height");
          }
          localEMMessage.addBody(localImageMessageBody);
        }
        else if (str1.equals("file"))
        {
          localEMMessage = new EMMessage(EMMessage.Type.FILE);
          localObject1 = localJSONObject2.getString("url");
          str2 = localJSONObject2.getString("filename");
          localObject3 = new NormalFileMessageBody(str2, (String)localObject1);
          ((NormalFileMessageBody)localObject3).fileSize = Integer.parseInt(localJSONObject2.getString("file_length"));
          if (localJSONObject2.has("localurl"))
            ((NormalFileMessageBody)localObject3).localUrl = localJSONObject2.getString("localurl");
          if (localJSONObject2.has("secret"))
            ((NormalFileMessageBody)localObject3).setSecret(localJSONObject2.getString("secret"));
          localEMMessage.addBody((MessageBody)localObject3);
        }
        else if (str1.equals("video"))
        {
          localEMMessage = new EMMessage(EMMessage.Type.VIDEO);
          localObject1 = localJSONObject2.getString("url");
          str2 = localJSONObject2.getString("filename");
          localObject3 = localJSONObject2.getString("thumb");
          int j = localJSONObject2.getInt("length");
          localObject5 = new VideoMessageBody(str2, (String)localObject1, (String)localObject3, j);
          if (localJSONObject2.has("localurl"))
            ((VideoMessageBody)localObject5).localUrl = localJSONObject2.getString("localurl");
          if (localJSONObject2.has("file_length"))
            ((VideoMessageBody)localObject5).file_length = localJSONObject2.getLong("file_length");
          if (localJSONObject2.has("thumblocalurl"))
            ((VideoMessageBody)localObject5).localThumb = localJSONObject2.getString("thumblocalurl");
          if (localJSONObject2.has("secret"))
            ((VideoMessageBody)localObject5).setSecret(localJSONObject2.getString("secret"));
          if (localJSONObject2.has("thumb_secret"))
            ((VideoMessageBody)localObject5).setThumbnailSecret(localJSONObject2.getString("thumb_secret"));
          localEMMessage.addBody((MessageBody)localObject5);
        }
        else
        {
          int i;
          if (str1.equals("audio"))
          {
            localEMMessage = new EMMessage(EMMessage.Type.VOICE);
            localObject1 = localJSONObject2.getString("url");
            str2 = localJSONObject2.getString("filename");
            i = localJSONObject2.getInt("length");
            VoiceMessageBody localVoiceMessageBody = new VoiceMessageBody(str2, (String)localObject1, i);
            if (localJSONObject2.has("localurl"))
              localVoiceMessageBody.localUrl = localJSONObject2.getString("localurl");
            if (localJSONObject2.has("secret"))
              localVoiceMessageBody.setSecret(localJSONObject2.getString("secret"));
            localEMMessage.addBody(localVoiceMessageBody);
          }
          else
          {
            Object localObject6;
            if (str1.equals("loc"))
            {
              localEMMessage = new EMMessage(EMMessage.Type.LOCATION);
              localObject1 = localJSONObject2.getString("addr");
              double d1 = localJSONObject2.getDouble("lat");
              double d2 = localJSONObject2.getDouble("lng");
              localObject6 = new LocationMessageBody((String)localObject1, d1, d2);
              localEMMessage.addBody((MessageBody)localObject6);
            }
            else if (str1.equals("cmd"))
            {
              localEMMessage = new EMMessage(EMMessage.Type.CMD);
              localObject1 = new HashMap();
              if (localJSONObject2.has("param"))
              {
                localObject2 = localJSONObject2.getJSONArray("param");
                for (i = 0; i < ((JSONArray)localObject2).length(); i++)
                {
                  localObject4 = ((JSONArray)localObject2).getJSONObject(i);
                  localObject5 = (String)((JSONObject)localObject4).keys().next();
                  localObject6 = (String)((JSONObject)localObject4).get((String)localObject5);
                  ((HashMap)localObject1).put(localObject5, localObject6);
                }
              }
              localObject2 = new CmdMessageBody(localJSONObject2.getString("action"), (HashMap)localObject1);
              localEMMessage.addBody((MessageBody)localObject2);
            }
          }
        }
      }
      if (localEMMessage != null)
      {
        localEMMessage.from = localEMContact1;
        localEMMessage.to = localEMContact2;
      }
      if (localJSONObject1.has("ext"))
      {
        localObject1 = localJSONObject1.getJSONObject("ext");
        localObject2 = ((JSONObject)localObject1).keys();
        while (((Iterator)localObject2).hasNext())
        {
          String str3 = (String)((Iterator)localObject2).next();
          localObject4 = ((JSONObject)localObject1).get(str3);
          if ((localObject4 instanceof String))
            localEMMessage.setAttribute(str3, (String)localObject4);
          else if ((localObject4 instanceof Integer))
            localEMMessage.setAttribute(str3, ((Integer)localObject4).intValue());
          else if ((localObject4 instanceof Boolean))
            localEMMessage.setAttribute(str3, ((Boolean)localObject4).booleanValue());
          else if ((localObject4 instanceof JSONObject))
            localEMMessage.setAttribute(str3, (JSONObject)localObject4);
          else if ((localObject4 instanceof JSONArray))
            localEMMessage.setAttribute(str3, (JSONArray)localObject4);
          else
            EMLog.e("msg", "unknow additonal msg attr:" + localObject4.getClass().getName());
        }
      }
      return localEMMessage;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return null;
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.MessageEncoder
 * JD-Core Version:    0.6.1
 */